package wks.reflect;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import wks.service.impl.CglibServiceImpl;

import java.lang.reflect.Method;

/**
 * cglib代理类
 */
public class CglibReflect implements MethodInterceptor {

    private CglibServiceImpl cglibService;

    public CglibReflect(CglibServiceImpl cglibService) {
        this.cglibService = cglibService;
    }

    public CglibServiceImpl getTargetBean() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibServiceImpl.class);
        enhancer.setCallback(this);
        return (CglibServiceImpl) enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("--------开始-------");
//        System.out.println(o);
//        String someThing = cglibService.doSomeThing();
        Object invoke = method.invoke(cglibService,objects);
        System.out.println("--------结束-------");
        return null;
    }
}
